<?php
namespace App\Http\Controllers\Business\App\QrCode;

use App\Models\Shop;
use App\Models\User;
use App\Models\SalesmanInfo;
use App\Models\ShopSalesman;

class QrCode
{
    //通过shopid或者二维码来返回二维码页面
    public static function shopQrCode($shopIdOrQrCode) {
        if(!$shopIdOrQrCode) {
            return ['status'=>'failed', 'message'=>'参数错误'];
        }
        $shopId = Shop::where('id', $shopIdOrQrCode)->orwhere('qrcode', $shopIdOrQrCode)->pluck('id')->first();
        //是不是地推商家
        $count = ShopSalesman::where('shopid', $shopId)->where('status', 1)->get()->count();
        if($count > 0) {
            //地推商家
            $shopName = Shop::where('id', $shopId)->pluck('name')->first();
            $data = [
                'type' => 'shopsalesman',
                'shopid' => $shopId,
                'shopname' => urldecode($shopName),
            ];
            return ['status'=>'success', 'data'=>$data];
        } else {
            $data = [
                'type' => 'downloadapp'
            ];
            return ['status'=>'success', 'data'=> $data];
        }
    }

    //通过userid或者二维码获取二维码页面
    public static function userQrCode($userIdOrQrCode) {
        if(!$userIdOrQrCode) {
            return ['status'=>'failed', 'message'=>'参数错误'];
        }
        $salesmanInfo = SalesmanInfo::where('salesmanid', $userIdOrQrCode)->orwhere('qrcode', $userIdOrQrCode)->first();
        if(!$salesmanInfo) {
            //不是地推用户
            $data = [
                'type' => 'downloadapp'
            ];
            return ['status'=>'success', 'data'=> $data];
        } else {
            //地推用户
            if($salesmanInfo->status == 0) {
                //状态被禁用
                $data = [
                    'type' => 'downloadapp'
                ];
                return ['status'=>'success', 'data'=> $data];
            }
            $userId = $salesmanInfo->salesmanid;
            $nickName = $salesmanInfo->name;
            $mobile = User::where('id', $userId)->pluck( 'mobile')->first();
            if(!$mobile) {
                //未注册
                $data = [
                    'type' => 'downloadapp'
                ];
                return ['status'=>'success', 'data'=> $data];
            } else {
                //已注册,
                //关闭入口
                $mobile = str_replace(substr($mobile, 3, 4), '****', $mobile);
                $data = [
                    'nickname' => $nickName,
                    'mobile' => $mobile,
                    'userid' => $userId,
                    'type' => 'usersalesman'
                ];
                return ['status'=>'success', 'data'=>$data];
            }
        }
    }
}